package com.shaoshaohuo.app.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Price;

import android.content.Context;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 市场行情 适配器
 * 
 * @author cao
 *
 */
public class MarketPriceAdapter extends MyBaseAdapter<Price> {

	public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public MarketPriceAdapter(Context ctx, List<Price> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View view, ViewGroup group) {
		if (view == null) {
			view = View.inflate(context, R.layout.item_price_fragment, null);
		}
		ViewHolder holder = ViewHolder.getHolder(view);
		Price marketPrice = list.get(pos);

		holder.tv_name.setText(marketPrice.getProductName());
		holder.tv_product.setText(marketPrice.getProductType());
		String string = "";
		String unit = "元/" + marketPrice.getUnit();
		if ("1".equals(marketPrice.getType())) {// 市场 选最低价
			string = string + marketPrice.getLowPrice() + unit;
		} else if ("2".equals(marketPrice.getType())) {// 产地选最高价
			string = string + marketPrice.getHighPrice() + unit;
		}
		SpannableString spannableString = new SpannableString(string);
		spannableString.setSpan(new AbsoluteSizeSpan(13, true), string.length() - unit.length(), string.length(),
				SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);
		holder.tv_unit.setText(spannableString);

		SpannableString spannable = new SpannableString(marketPrice.getMoney() + "元");
		spannable.setSpan(new AbsoluteSizeSpan(13, true), spannable.length() - 1, spannable.length(),
				SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);

//		Log.e("tag", marketPrice.getPriceType()+"++++++++++++++++++");
		
		
		if ("3".equals(marketPrice.getPriceType())) {
			holder.iv_ping.setVisibility(View.VISIBLE);
			holder.tv_sheng_unit.setVisibility(View.GONE);
			holder.tv_jiang_unit.setVisibility(View.GONE);
		} else if ("1".equals(marketPrice.getPriceType())) {
			holder.iv_ping.setVisibility(View.GONE);
			holder.tv_sheng_unit.setVisibility(View.VISIBLE);
			holder.tv_jiang_unit.setVisibility(View.GONE);
			holder.tv_sheng_unit.setText(spannable);
		} else if ("2".equals(marketPrice.getPriceType())) {
			holder.iv_ping.setVisibility(View.GONE);
			holder.tv_sheng_unit.setVisibility(View.GONE);
			holder.tv_jiang_unit.setVisibility(View.VISIBLE);
			holder.tv_jiang_unit.setText(spannable);
			
		}

		holder.tv_address.setText(marketPrice.getAddressInfo());
		holder.tv_time.setText(dateFormat.format(new Date(Long.parseLong(marketPrice.getInsertTime()))));
		return view;
	}

	static class ViewHolder {
		public TextView tv_name;
		public TextView tv_product;
		public TextView tv_unit;
		public TextView tv_sheng_unit;
		public TextView tv_jiang_unit;
		public ImageView iv_ping;
		public TextView tv_address;
		public TextView tv_time;

		public ViewHolder(View view) {
			tv_jiang_unit = (TextView) view.findViewById(R.id.tv_jiang_unit);
			tv_name = (TextView) view.findViewById(R.id.tv_name);
			tv_product = (TextView) view.findViewById(R.id.tv_product);
			tv_unit = (TextView) view.findViewById(R.id.tv_unit);
			tv_sheng_unit = (TextView) view.findViewById(R.id.tv_sheng_unit);
			iv_ping = (ImageView) view.findViewById(R.id.iv_ping);
			tv_address = (TextView) view.findViewById(R.id.tv_address);
			tv_time = (TextView) view.findViewById(R.id.tv_time);
			view.setTag(this);
		}

		public static ViewHolder getHolder(View view) {
			return (ViewHolder) (view.getTag() != null ? view.getTag() : new ViewHolder(view));
		}
	}

}
